package com.vsked.util;

import java.io.Serializable;
import java.sql.Connection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.vsked.bean.ConnectionInfo;
import com.vsked.connection.ConnectionUtil;
import com.vsked.database.ColumnInfo;
import com.vsked.database.TableInfo;

public class BeanProcess {
	public static String forDAOInfoMethodString="";
	public static String addConsructString="";
	public static String editConsstructString="";
	public static String addNewObjectInParmForServlet="";
	public static String editNewObjectInParmForServlet="";
	
	public static String getFieldDeclare(String inFieldName,String inDataType){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"private "+inDataType+" "+Symbol.getStringFirstLowerCase(inFieldName)+";"+Symbol.enterSymbol;		
	}
	public static String getObjectDeclareString(String inTableName){
		return Symbol.getStringFirstUpperCase(inTableName)+Symbol.getSpaceString(1)+Symbol.getStringFirstLowerCase(inTableName);
	}
	public static String getInObjectDeclareString(String inTableName){
		return Symbol.getStringFirstUpperCase(inTableName)+Symbol.getSpaceString(1)+"in"+Symbol.getStringFirstUpperCase(inTableName);
	}
	public static String getFieldGetterMethod(String inFieldName,String inDataType){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public "+inDataType+" get"+Symbol.getStringFirstUpperCase(inFieldName)+"(){"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"return "+Symbol.getStringFirstLowerCase(inFieldName)+";"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}"+Symbol.enterSymbol;
	}
	
	public static String getFieldSetterMethod(String inFieldName,String inDataType){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public void set"+Symbol.getStringFirstUpperCase(inFieldName)+"("+inDataType+Symbol.getSpaceString(1)+ Symbol.getStringFirstLowerCase(inFieldName)+"){"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"this."+Symbol.getStringFirstLowerCase(inFieldName)+Symbol.getSpaceString(1)+"="+Symbol.getSpaceString(1)+Symbol.getStringFirstLowerCase(inFieldName)+";"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}"+Symbol.enterSymbol;
	}
	
	public static String getClassForObject(String inTableName,String inClassContent){
		return Symbol.enterSymbol+"public class "+Symbol.getStringFirstUpperCase(inTableName)+" implements java.io.Serializable {"+Symbol.enterSymbol+inClassContent+Symbol.enterSymbol+"}"+Symbol.enterSymbol;
	}
	public static String getNewObject(String inTableName){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+BeanProcess.getObjectDeclareString(inTableName)+Symbol.getSpaceString(1)+"="+Symbol.getSpaceString(1)+NewObjectString(inTableName);
	}
	public static String NewObjectString(String inTableName){
		return "new"+Symbol.getSpaceString(1)+Symbol.getStringFirstUpperCase(inTableName)+"();";
	}
	public static String getReturnObject(String inTableName){
		return Symbol.enterSymbol+Symbol.getSpaceString(1)+"return"+Symbol.getSpaceString(1)+Symbol.getStringFirstLowerCase(inTableName)+";";
	}
	public static String getObjectFiledSet(String inTableName,String inFieldName,String inDataType){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+Symbol.getStringFirstLowerCase(inTableName)+".set"+Symbol.getStringFirstUpperCase(inFieldName)+"(rs.get"+Symbol.getStringFirstUpperCase(inDataType)+"(\""+inFieldName+"\"));";		
	}
	public static String getBeanFileContent(ConnectionInfo ci,String inTableName) throws Exception{
		String beanStrAll="";
		Connection conn = null;
		ColumnInfo columnInfo=new ColumnInfo();
		beanStrAll="";
		String beanDeclareStr="";
		String beanSetterAndGetter="";
		List<Map> columnName=columnInfo.getTableColumnNameAndDataType(ci, inTableName);
		DaoProcess.ProcessDAOConnection(ci);
		if("MsSqlServer2000".equals(ci.getDbType())){
			conn=new ConnectionUtil().getSqlServer2000Connection(ci);
			for(int columnIndex=0;columnIndex<columnName.size();columnIndex++){
				Map columnMap=columnName.get(columnIndex);
				Iterator it=columnMap.entrySet().iterator();
				while(it.hasNext()){
					Map.Entry me=(Map.Entry)it.next();
					String columnNameStr=Symbol.replaceSymbol(me.getKey().toString());
					String inColumn=columnNameStr;
					String inColumnDataType=TransFormDataType.getSqlServerDataType(Integer.parseInt(me.getValue().toString()));
					beanDeclareStr+=BeanProcess.getFieldDeclare(inColumn,inColumnDataType);
					beanSetterAndGetter+=BeanProcess.getFieldGetterMethod(inColumn,inColumnDataType);
					beanSetterAndGetter+=BeanProcess.getFieldSetterMethod(inColumn, inColumnDataType);
				}//end while
			}//end columnIndex
		}//end if MsSqlServer2000
		
		if("MySql".equals(ci.getDbType())){
			conn=new ConnectionUtil().getMySqlConnection(ci);
			for(int columnIndex=0;columnIndex<columnName.size();columnIndex++){
				Map columnMap=columnName.get(columnIndex);
				Iterator it=columnMap.entrySet().iterator();
				while(it.hasNext()){
					Map.Entry me=(Map.Entry)it.next();
					String columnNameStr=Symbol.replaceSymbol(me.getKey().toString());
					String inColumn=columnNameStr;
					String inColumnDataType=TransFormDataType.getMySqlDataType(me.getValue().toString());
					beanDeclareStr+=BeanProcess.getFieldDeclare(inColumn,inColumnDataType);
					beanSetterAndGetter+=BeanProcess.getFieldGetterMethod(inColumn,inColumnDataType);
					beanSetterAndGetter+=BeanProcess.getFieldSetterMethod(inColumn, inColumnDataType);
				}//end while
			}//end columnIndex
		}//end if MySql

		beanStrAll=BeanProcess.getClassForObject(inTableName,beanDeclareStr+beanSetterAndGetter+Symbol.enterSymbol+BeanProcess.getConstructNull(inTableName)+Symbol.enterSymbol+BeanProcess.addConsructString+Symbol.enterSymbol+BeanProcess.editConsstructString);
		return beanStrAll;
	}
	public static String getConstructNull(String inTableName){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public "+Symbol.getStringFirstUpperCase(inTableName)+"(){"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static String getBeanFileContentByContruct(ConnectionInfo ci,String inTableName,String inConstruct) throws Exception{
		String beanStrAll="";
		Connection conn = new ConnectionUtil().getSqlServer2000Connection(ci);
        ColumnInfo columnInfo=new ColumnInfo();
			beanStrAll="";
			String beanDeclareStr="";
			String beanSetterAndGetter="";
			List<Map> columnName=columnInfo.getTableColumnNameAndDataType(ci, inTableName);
			for(int columnIndex=0;columnIndex<columnName.size();columnIndex++){
				Map columnMap=columnName.get(columnIndex);
				Iterator it=columnMap.entrySet().iterator();
				while(it.hasNext()){
					Map.Entry me=(Map.Entry)it.next();
					String columnNameStr=Symbol.replaceSymbol(me.getKey().toString());
					String inColumn=columnNameStr;
					String inColumnDataType=TransFormDataType.getSqlServerDataType(Integer.parseInt(me.getValue().toString()));
					beanDeclareStr+=BeanProcess.getFieldDeclare(inColumn,inColumnDataType);
					beanSetterAndGetter+=BeanProcess.getFieldGetterMethod(inColumn,inColumnDataType);
					beanSetterAndGetter+=BeanProcess.getFieldSetterMethod(inColumn, inColumnDataType);
				}//end while
			}//end columnIndex
			beanStrAll=BeanProcess.getClassForObject(inTableName,beanDeclareStr+beanSetterAndGetter+inConstruct);
		return beanStrAll;
	}
	public static String getContructFromArray(String inTableName,String[] inColumnArray){
		String constructString="";
		String inParameter="";
		String inSet="";
		for(int i=0;i<inColumnArray.length;i++){
			String inFieldName=inColumnArray[i].split("#")[0];
			String inFieldDataType=inColumnArray[i].split("#")[1];
            inParameter="".equals(inParameter)?inFieldDataType+Symbol.getSpaceString(1)+Symbol.getStringFirstLowerCase(inFieldName):inParameter+","+inFieldDataType+Symbol.getSpaceString(1)+Symbol.getStringFirstLowerCase(inFieldName);
            inSet+=Symbol.enterSymbol+Symbol.getSpaceString(8)+"this."+Symbol.getStringFirstLowerCase(inFieldName)+Symbol.getSpaceString(1)+"="+Symbol.getSpaceString(1)+Symbol.getStringFirstLowerCase(inFieldName)+";";
		}
		inParameter="("+inParameter+")";
		constructString=Symbol.getSpaceString(4)+"public "+Symbol.getStringFirstUpperCase(inTableName)+inParameter+"{"+inSet+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
		return constructString;		
	}
	public static String getNewObjectForServlet(String inTableName,String inParameter){
		return "new"+Symbol.getSpaceString(1)+Symbol.getStringFirstUpperCase(inTableName)+"("+inParameter+")";
	}
	public static void main(String[] args) throws Exception {
		System.out.println(BeanProcess.getConstructNull("TbUser"));
		
	}
	
}
